package com.addcn.addcnwebview.webview;

import android.net.http.SslError;
import android.util.Log;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;

import com.addcn.addcnwebview.BuildConfig;

import java.util.concurrent.atomic.AtomicBoolean;

import ren.yale.android.cachewebviewlib.WebViewCacheInterceptorInst;


/**
 * Created by 10528 on 2016/1/4.
 */
public  class CustomWebViewClient extends WebViewClient {
    private boolean networkImage = false;
    private static AtomicBoolean crash = new AtomicBoolean(true);
    public void setNetworkImage(boolean networkImage) {
        this.networkImage = networkImage;
    }


    @Override
    public void onPageFinished(WebView webView, String s) {
        super.onPageFinished(webView, s);
        if (networkImage){
            webView.getSettings().setBlockNetworkImage(false);
        }
        Log.d(this.getClass().getName(),"==onPageFinished");
        webView.loadUrl("javascript:App.resize(document.body.getBoundingClientRect().height)");
        webView.loadUrl("javascript:var objs=document.querySelectorAll('.lazy-loading');var img=new Array();for(var i=0;i<objs.length;i++){var obj={};obj.src=objs[i].src;obj.alt=objs[i].alt;img.push(obj)}for(var i=0;i<objs.length;i++){objs[i].onclick=function(){console.log(img);var obj=img[0];var src=this.src;var link=(this.parentElement.tagName=='A').toString();setupWebViewJavascriptBridge(function(bridge){bridge.callHandler('openPhotos',{'image':src,'images':img,'hasLink':link,},function responseCallback(responseData){responseCallback(data)})})}}");
//        if (crash.compareAndSet(true, false)) {
//            crashPage(webView);
//        }
    }

    protected void crashPage(WebView view) {
        // Do something bad that will crash the renderer process
        view.evaluateJavascript("javascript:(function() { txt = \"a\"; while(1){ txt += \"a\"; } })();", null);
    }

    @Override
    public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
        if (BuildConfig.DEBUG){
            sslErrorHandler.proceed();
        }else{
            super.onReceivedSslError(webView, sslErrorHandler, sslError);
        }
    }



    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        if (BuildConfig.DEBUG){
            return super.shouldInterceptRequest(view, url);
        }
        return  WebViewCacheInterceptorInst.getInstance().interceptRequest(url);
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
        if (BuildConfig.DEBUG){
            return super.shouldInterceptRequest(webView, webResourceRequest);
        }
        return  WebViewCacheInterceptorInst.getInstance().interceptRequest( webResourceRequest);
    }

    @Override
    public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
       // Log.d("==onRenderProcessGone:"+onRenderProcessGone(view, detail));
        return super.onRenderProcessGone(view, detail);
        //return true;
    }
}
